//
//  OBNavigationControllerStack.m
//  XZXSchool
//
//  Created by obally on 2017/10/23.
//  Copyright © 2017年 obally. All rights reserved.
//

#import "OBNavigationControllerStack.h"
#import "OBViewController.h"
#import "OBRouter.h"
#import "OBNavigationController.h"
#import "OBTabBarController.h"
//#import "OBTabBarController.h"
@interface OBNavigationControllerStack()
@property(nonatomic,strong) id<OBViewModelServices> services;
@property(nonatomic,strong) NSMutableArray *navigationControllers;


@end
@implementation OBNavigationControllerStack
- (instancetype)initWithServices:(id<OBViewModelServices>)services
{
    self = [super init];
    if (self) {
        self.services = services;
        self.navigationControllers = [[NSMutableArray alloc]init];
        [self registerNavigationHooks];
    }
    return self;
    
}
- (void)pushNavigationController:(UINavigationController *)navigationController {
    
    if ([self.navigationControllers containsObject:navigationController]) return;
    
    [self.navigationControllers addObject:navigationController];
}

- (UINavigationController *)popNavigationController {
    UINavigationController *navigationController = self.navigationControllers.lastObject;
    [self.navigationControllers removeLastObject];
    return navigationController;
}

- (UINavigationController *)topNavigationController {
    return self.navigationControllers.lastObject;
}
- (void)registerNavigationHooks
{
    @weakify(self);
    //响应pushViewController:animated: 方法
    [[(NSObject *)self.services rac_signalForSelector:@selector(pushViewModel:animated:)]subscribeNext:^(RACTuple *tuple) {
        @strongify(self);
        OBViewController *topViewController = (OBViewController *)[self.navigationControllers.lastObject topViewController];
        if (topViewController.tabBarController) {
            topViewController.snapshot = [topViewController.tabBarController.view snapshotViewAfterScreenUpdates:NO];
        } else {
            topViewController.snapshot = [[self.navigationControllers.lastObject view]snapshotViewAfterScreenUpdates:NO];
        }
        UIViewController *viewController = (UIViewController *)[OBRouter.shareInstance viewControllerForViewModel:tuple.first];
        [self.navigationControllers.lastObject pushViewController:viewController animated:YES];
    }];
    [[(NSObject *)self.services rac_signalForSelector:@selector(popViewModelAnimated:)]subscribeNext:^(RACTuple *tuple) {
        @strongify(self);
        [self.navigationControllers.lastObject popViewControllerAnimated:[tuple.first boolValue]];
    }];
    [[(NSObject *)self.services
      rac_signalForSelector:@selector(popToRootViewModelAnimated:)]
     subscribeNext:^(RACTuple *tuple) {
         @strongify(self)
         [self.navigationControllers.lastObject popToRootViewControllerAnimated:[tuple.first boolValue]];
     }];
    [[(NSObject *)self.services rac_signalForSelector:@selector(presentViewModel:animated:completion:)]subscribeNext:^(RACTuple *tuple) {
        @strongify(self);
        UIViewController *controller = (UIViewController *)[OBRouter.shareInstance viewControllerForViewModel:tuple.first];
        UINavigationController *presentingViewController = self.navigationControllers.lastObject;
        if (![controller isKindOfClass:UINavigationController.class]) {
            controller = [[OBNavigationController alloc]initWithRootViewController:controller];
        }
        [self pushNavigationController:(UINavigationController *)controller];
        [presentingViewController presentViewController:controller animated:[tuple.second boolValue] completion:tuple.third];
        
    }];
    [[(NSObject *)self.services
      rac_signalForSelector:@selector(dismissViewModelAnimated:completion:)]
     subscribeNext:^(RACTuple *tuple) {
         @strongify(self)
         [self popNavigationController];
         [self.navigationControllers.lastObject dismissViewControllerAnimated:[tuple.first boolValue] completion:tuple.second];
     }];
    
    [[(NSObject *)self.services
      rac_signalForSelector:@selector(resetRootViewModel:)]
     subscribeNext:^(RACTuple *tuple) {
         @strongify(self)
         [self.navigationControllers removeAllObjects];
         /// VM映射VC
         UIViewController *viewController = (UIViewController *)[OBRouter.shareInstance viewControllerForViewModel:tuple.first];
         if (![viewController isKindOfClass:[UINavigationController class]] &&
             ![viewController isKindOfClass:[OBTabBarController class]]) {
             viewController = [[OBNavigationController alloc] initWithRootViewController:viewController];
             [self pushNavigationController:(UINavigationController *)viewController];
         }
         [UIApplication sharedApplication].delegate.window.rootViewController = viewController;
     }];
}
@end
